<?php

namespace Xrfg\Core\Trading\Spot\Models;

use Illuminate\Database\Eloquent\Model;
use Xrfg\Core\Trading\Account\Models\Coin;
use Xrfg\Core\User\Models\User;

/**
 * Xrfg\Core\Trading\Spot\Models\Order.
 *
 * @property int                                                                             $id          订单ID
 * @property string                                                                          $symbol      交易对符号
 * @property int                                                                             $user_id     用户ID
 * @property int                                                                             $coin_id     币种ID
 * @property int                                                                             $market_id   交易区ID
 * @property string                                                                          $side        方向
 * @property string                                                                          $type        类型
 * @property float|null                                                                      $price       价格
 * @property float                                                                           $number      数量
 * @property float                                                                           $fee         手续费
 * @property float|null                                                                      $deal_number 已成交数量
 * @property float|null                                                                      $total       已成交金额
 * @property int|null                                                                        $status      订单状态：0-未完全成交，1-完全成交，2-已撤销
 * @property \Illuminate\Support\Carbon|null                                                 $created_at  挂单时间
 * @property \Illuminate\Support\Carbon|null                                                 $updated_at  最后操作时间
 * @property \Illuminate\Database\Eloquent\Collection|\Xrfg\Core\Trading\Spot\Models\Trade[] $buyTrades
 * @property \Xrfg\Core\Trading\Account\Models\Coin                                          $coin
 * @property \Illuminate\Database\Eloquent\Collection|\Xrfg\Core\Trading\Spot\Models\Trade[] $sellTrades
 * @property \Xrfg\Core\User\Models\User                                                     $user
 *
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order query()
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereCoinId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereDealNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereMarketId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereSide($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereSymbol($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereTotal($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\Xrfg\Core\Trading\Spot\Models\Order whereUserId($value)
 * @mixin \Eloquent
 */
class Order extends Model
{
    protected $table = 'trading_spot_orders';
    /**
     * @var int 类编码
     */
    const CLASS_ID = 4;
    /**
     * @var int 操作编码：币币买入挂单
     */
    const OP_BUY_NEW = 1;
    /**
     * @var int 操作编码：币币买入撤销
     */
    const OP_BUY_CANCELED = 2;
    /**
     * @var int 操作编码：币币卖出挂单
     */
    const OP_SELL_NEW = 3;
    /**
     * @var int 操作编码：币币卖出撤销
     */
    const OP_SELL_CANCELED = 4;

    /**
     * @var int 未成交
     */
    const STATUS_NEW = 0;
    /**
     * @var int 完全成交
     */
    const STATUS_FILLED = 1;
    /**
     * @var int 已撤销
     */
    const STATUS_CANCELED = 2;

    const SIDE_BUY = 'BUY';

    const SIDE_SELL = 'SELL';

    const TYPE_LIMIT = 'LIMIT';

    const TYPE_MARKET = 'MARKET';

    public function buyTrades()
    {
        return $this->hasMany(Trade::class, 'buy_order_id', 'id');
    }

    public function sellTrades()
    {
        return $this->hasMany(Trade::class, 'sell_order_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function coin()
    {
        return $this->belongsTo(Coin::class);
    }

    public function market()
    {
        return $this->belongsTo(Pair::class, 'symbol', 'symbol');
    }
}
